package com.t2c.tibetantochinese.base;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;

import com.t2c.tibetantochinese.views.CustomDialog;
import com.umeng.analytics.MobclickAgent;


public abstract class BaseActivity extends FragmentActivity{

    private CustomDialog mLoadingDialog;//网络请求
    private Boolean mActivityOnPause = false;//APP是否在后台
    private Boolean mNeedStopLoading = false;//APP从后台返回时,如果LOADING还在就干掉它

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentViewResId());
    }

    protected abstract int getContentViewResId();

    /**
     * 网络请求过程开始
     */
    protected void startLoadingProgress(String hint){
        mLoadingDialog = new CustomDialog(this, CustomDialog.DialogType.Loading);
        mLoadingDialog.setLoadingText(hint);
        mLoadingDialog.show();
    }

    /**
     * 网络请求结束
     */
    protected void stopLoadingProgress(){
        if (mLoadingDialog != null && mLoadingDialog.isShowing() ){
            if(!mActivityOnPause){
                mLoadingDialog.dismiss();
                mLoadingDialog = null;
                mNeedStopLoading = false;
            }else{
                mNeedStopLoading =true;
            }
        }
    }

    //注释掉，让其不再保存Fragment的状态，达到其随着Activity一起被回收的效果！
    @Override
    protected void onSaveInstanceState(Bundle outState) {
    }

    public void onback(View view){
        finish();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        mActivityOnPause = false;
        if(mNeedStopLoading){
            stopLoadingProgress();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        mActivityOnPause = true;
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
}
